xlv.obj.user={
	extend:function(){
		this.extend('xlv.obj.webObject');
	},
	implement:function(){
	},
	properties:{
		name:'user',
		objectURL:'user',
		globe:true,
		propXPath:'properties',
		idAuthorized:'@xlive' //
	},
	method:{
		initial:function(){
			this.$super('xlv.obj.webObject.initial');
		},
		_idAuthorizedCheck:function(user_id){
			if(!user_id.endsWith(this.$P.idAuthorized)){
				alert('user id must end with :'+this.$P.idAuthorized);
				return false;
			}
			return true;
		},
		login:function(){
			var method_node=this.xml_selectSingleNode(this.$P.propXPath+'/login',null);
			if(method_node){
				var user_id=this.getValue('login.user-id');
				if(!this._idAuthorizedCheck(user_id)) return;
				var return_node=this.xml_selectSingleNode('return',method_node);
				if(return_node)return_node.parentNode.removeChild(return_node);
				var user_pass=this.getValue('login.user-pass');
				var hash=this.getValue('login.hash');
				user_pass=xlv.util.hash(user_pass);
				user_pass=xlv.util.hash(hash+user_pass);
				var arg_node=method_node.cloneNode(true);
				var n = this.xml_selectSingleNode('user-pass',arg_node);
				this.xml_setNodeValue(n,user_pass);
				n=this.xml_selectSingleNode('hash',arg_node);
				if(n) n.parentNode.removeChild(n);
				setTimeout((function(){this.request(null,'login',arg_node);}).bind(this), 1);
			}
		},
		loginOauth:function(){
			var method_node=this.xml_selectSingleNode(this.$P.propXPath+'/login-oauth',null);
			if(method_node){
				xlv.util.removeCookie('token-secret');
				xlv.util.removeCookie('access-token');
				this.setValue(document.domain,'login-oauth.domain');
				this.setValue(location.protocol, 'login-oauth.location.protocol');
				this.setValue(location.hostname,'login-oauth.location.hostname');
				this.setValue(location.host,'login-oauth.location.host');
				this.setValue(location.port,'login-oauth.location.port');
				this.setValue(location.pathname,'login-oauth.location.pathname');
				this.setValue(location.href,'login-oauth.location.href');
				this.webMethod('login-oauth');
			}
		},
		_oauthMonitor:function(){
			var v=this.$V;
			var at=xlv.util.getCookie('access-token');
			if(at){
				if('failure'==at){
				}else{
				}
				v._oauth_win.close();
			}
			if(v._oauth_win.closed)	clearInterval(v._interval);
		},
		isOauth:function(){
			return "true" == this.getValue('login-oauth.return.valid');
		},
		openidLogin:function(){
			this.getDesktop().showPane('/xlive/security/loginOpenId.html');
		},
		openidQueryString:function(){
			var endpoint=this.getValue('openid-endpoint');
			var openid=this.getNode('openid');
			var nodes=openid.getElementsByTagName('*');
			var qstr='';
			for(var i=0;i<nodes.length;++i){
				var c=nodes[i];
				var v=xlv.util.getXmlText(c);
				if(v.length<=0||xlv.util.hasElementChildNode(c))continue;
				var name='';
				while(c!=openid){
					name='.'+c.nodeName+name;
					c=c.parentNode;
				}
				name=openid.nodeName+name;
				qstr=qstr+(qstr.length>0?'&':'')+name+'='+encodeURIComponent(v);
			}
			qstr=endpoint+'?'+qstr;
			return qstr;
		},
		isPasswordValid:function(){
			return (this.xml_properties.currentXML && 'true'==this.getValue('login.return.valid'));
		},
		register:function(){
			var method_node=this.xml_selectSingleNode(this.$P.propXPath+'/register',null);
			if(method_node){
				var user_id=this.getValue('register.data.user-id');
				if(!this._idAuthorizedCheck(user_id)) return;
				setTimeout((function(){this.request(null,'register',method_node.cloneNode(true));}).bind(this), 1);
			}
		},
		logout:function(){
			var auth_node=this.xml_selectSingleNode(this.$P.propXPath+'/login/return/data/authorized');
			if(auth_node)auth_node.parentNode.removeChild(auth_node);
			var method_node=this.xml_selectSingleNode(this.$P.propXPath+'/logout',null);
			if(method_node){
				setTimeout((function(){this.request(null,'logout',method_node.cloneNode(true));}).bind(this), 1);
			}
		},
		webObjectResponse:function(){
			this.$super('xlv.obj.webObject.webObjectResponse');
			if(this.xml_responseMethodNames()=='login-oauth'){
				var url=this.getValue('login-oauth.return.auth-url');
				this.$V._oauth_win=window.open(url,'OAuth', 'channelmode=yes,width=600,height=450,alwaysRaised=yes');
				this.$V._interval=setInterval(function(){this._oauthMonitor();}.bind(this),300);
			}
		}
	}
};

